using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using System.IO;
using System.Xml;
using System.Resources;
using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Administration;

namespace TweakSP.Web.Admin
{
    public partial class TweakSP_AllFeatures : GlobalAdminPageBase
    {
        protected SPGridView allFeatures;

        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder output = new StringBuilder();

            DataTable featureTbl = LoadFeatures();

            ConfigureGridView();

            allFeatures.DataSource = featureTbl;

            allFeatures.DataBind();

        }

        void ConfigureGridView()
        {
            SPBoundField col;
            
            col = new SPBoundField();
            col.DataField = "Title";
            col.HeaderText = "Title";
            allFeatures.Columns.Add(col);

            col = new SPBoundField();
            col.DataField = "Id";
            col.HeaderText = "Feature Id";
            allFeatures.Columns.Add(col);

            col = new SPBoundField();
            col.DataField = "Scope";
            col.HeaderText = "Feature Scope";
            allFeatures.Columns.Add(col);

            allFeatures.GroupField = "Scope";
            allFeatures.GroupFieldDisplayName = "Feature Scope";
            allFeatures.AllowGrouping = true;
        }


        DataTable LoadFeatures()
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("Title");
            tbl.Columns.Add("Id");
            tbl.Columns.Add("Scope");

            foreach (SPFeatureDefinition def in SPFarm.Local.FeatureDefinitions)
            {
                DataRow row = tbl.NewRow();
                row["Title"] = def.DisplayName;
                row["Id"] = def.Id.ToString();
                row["Scope"] = def.Scope.ToString();

                tbl.Rows.Add(row);
            }

            tbl.DefaultView.Sort = "Scope, Title";

            return tbl;
        }




    }
}
